// 邻里互助API服务
const baseURL = 'http://localhost:3002/api'; // 后端API地址，请根据实际情况修改

const NeighborhoodAPI = {
  /**
   * 登记闲置物品
   * @param {Object} itemData - 物品数据
   */
  registerIdleItem: function(itemData) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${baseURL}/idle-items`,
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        data: itemData,
        success: (res) => {
          if (res.statusCode === 200 || res.statusCode === 201) {
            resolve({
              success: true,
              message: '物品登记成功，等待管理员审核',
              data: res.data.data || res.data
            });
          } else {
            reject(new Error(res.data.message || '登记失败'));
          }
        },
        fail: (err) => {
          console.error('API请求失败:', err);
          reject(err);
        }
      });
    });
  },

  /**
   * 发布技能共享
   * @param {Object} skillData - 技能数据
   */
  publishSkill: function(skillData) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${baseURL}/skills`,
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        data: skillData,
        success: (res) => {
          if (res.statusCode === 200 || res.statusCode === 201) {
            resolve({
              success: true,
              message: '技能发布成功',
              data: res.data.data || res.data
            });
          } else {
            reject(new Error(res.data.message || '发布失败'));
          }
        },
        fail: (err) => {
          console.error('API请求失败:', err);
          reject(err);
        }
      });
    });
  },

  /**
   * 获取闲置物品详情
   * @param {String} itemId - 物品ID
   */
  getIdleItemDetail: function(itemId) {
    return new Promise((resolve, reject) => {
      wx.request({
        url: `${baseURL}/idle-items/${itemId}`,
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          if (res.statusCode === 200) {
            resolve({
              success: true,
              data: res.data.data || res.data
            });
          } else {
            reject(new Error(res.data.message || '获取数据失败'));
          }
        },
        fail: (err) => {
          console.error('API请求失败:', err);
          reject(err);
        }
      });
    });
  },

  /**
   * 获取闲置物品列表
   * @param {Object} params - 查询参数
   */
  getIdleItemsList: function(params = {}) {
    return new Promise((resolve, reject) => {
      // 构建查询参数
      const queryParams = new URLSearchParams();
      if (params.page) queryParams.append('page', params.page);
      if (params.category) queryParams.append('category', params.category);
      if (params.duration) queryParams.append('duration', params.duration);
      
      wx.request({
        url: `${baseURL}/idle-items?${queryParams.toString()}`,
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          if (res.statusCode === 200) {
            resolve({
              success: true,
              data: res.data.data || [],
              total: res.data.total || 0
            });
          } else {
            reject(new Error(res.data.message || '获取数据失败'));
          }
        },
        fail: (err) => {
          console.error('API请求失败:', err);
          reject(err);
        }
      });
    });
  },

  /**
   * 获取技能共享列表
   * @param {Object} params - 查询参数
   */
  getSkillsList: function(params = {}) {
    return new Promise((resolve, reject) => {
      // 构建查询参数
      const queryParams = new URLSearchParams();
      if (params.page) queryParams.append('page', params.page);
      if (params.category) queryParams.append('category', params.category);
      if (params.serviceMethod) queryParams.append('serviceMethod', params.serviceMethod);
      
      wx.request({
        url: `${baseURL}/skills?${queryParams.toString()}`,
        method: 'GET',
        header: {
          'Content-Type': 'application/json'
        },
        success: (res) => {
          if (res.statusCode === 200) {
            resolve({
              success: true,
              data: res.data.data || [],
              total: res.data.total || 0
            });
          } else {
            reject(new Error(res.data.message || '获取数据失败'));
          }
        },
        fail: (err) => {
          console.error('API请求失败:', err);
          reject(err);
        }
      });
    });
  },

  /**
   * 申请借用物品
   * @param {Object} borrowData - 借用数据
   */
  borrowItem: function(borrowData) {
    return new Promise((resolve, reject) => {
      // 模拟API调用
      setTimeout(() => {
        resolve({
          success: true,
          message: '借用申请已发送，请等待对方确认',
          data: {
            id: Date.now(),
            status: 'pending',
            ...borrowData
          }
        });
      }, 1000);
    });
  },

  /**
   * 联系技能提供者
   * @param {Object} contactData - 联系数据
   */
  contactSkillProvider: function(contactData) {
    return new Promise((resolve, reject) => {
      // 模拟API调用
      setTimeout(() => {
        resolve({
          success: true,
          message: '联系信息已发送',
          data: {
            id: Date.now(),
            ...contactData
          }
        });
      }, 1000);
    });
  }
};

module.exports = NeighborhoodAPI;